#define _GNU_SOURCE
#include "../include/pthreadpool.h"

int getsfileplus_command(int peerfd, const char* pthname){
    int fds[2];
    pipe(fds);
    size_t hassend=0,filelength=0;
    int ret=recv(peerfd,&hassend,8,MSG_WAITALL);
    if(ret!=8) return ret;
    int fd=open(pthname,O_RDWR);
    if(fd==-1) return -1;
    int off_set=lseek(fd,hassend,SEEK_SET);
    if(off_set==-1) return -1;
    struct stat st;
    memset(&st,0,sizeof(st));
    fstat(fd, &st);
    filelength=st.st_size-hassend;
    ret=send(peerfd, &filelength, 8, MSG_WAITALL);
    printf("ret=%d\n",ret);
    if(ret<=0) return ret;
    printf("filelength=%ld\n",filelength);
    size_t sendSize = 0;
    while(sendSize < filelength){
        ret=splice(fd,NULL,fds[1],NULL,4096,SPLICE_F_MORE);
        ret=splice(fds[0],NULL,peerfd,NULL,ret,SPLICE_F_MORE);
        if(ret<=0) return ret;
        sendSize+=ret;
    }
    printf("has downlown!\n");
    return 1;
} 
